<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <button>send</button>
    </div>
    <script>
      // 创建WebSocket连接
      const socket = new WebSocket("ws://localhost:3000");
      // 连接成功时触发
      socket.onopen = (event) => {
        console.log("WebSocket连接已经建立");
        // 发送消息给服务器
        socket.send("Hello, WebSocket Server!");
      };
      // 接收到消息时触发
      socket.onmessage = (event) => {
        console.log("收到服务器消息： " + event.data);
      };
      // 连接关闭时触发
      socket.onclose = (event) => {
        console.log("WebSocket连接已关闭");
      };
    </script>
  </body>
</html>
